﻿@model IEnumerable<CmsMedia>

@functions {
    string GetUrl(CmsMedia item)
    {
        if (!item.IsNew)
        {
            return Url.Action("get", "cmsmedia", new {id = @item.Id});
        }

        return Url.Action("Index", "cmsmedia");
    }
}

<div class="b-media g-clear">@foreach (var item in Model)
                             {<div class="b-media__item" data-id="@item.Id" data-type="ChangeFolderParent">
            <a class="b-media__link" href='@GetUrl(item)'>
            	<span class="b-icon b-icon_media">
            	@if (item.TotalMediaContent > 0)
             {
                    <b class="b-media__num">@item.TotalMediaContent</b>
             }
             else if(item.TotalMediaContent == -1)
                  {
                      <b class="b-media__num">..</b>
                  }
            	</span>
            	<span class="b-media__name">
            		@if (item.TotalMediaContent != -1)
                    {
                        @item.FolderName
                    }
            	</span>
            </a>
            <p class="b-media__description">@item.Description</p>
        	<input type="hidden" value="@item.DateCreated" /></div>}</div>

<script type="text/javascript">
    $(document).ready(function () {
        initDraggable(".b-media__item");

        $(".b-media__item").bind("dragover", function (e) {
            if ($(this).data("id") == 0 && e.originalEvent.dataTransfer.getData("Action") == "ChangeContentFolder")
            { return true; }
            
            if (e.originalEvent.preventDefault) {
                e.originalEvent.preventDefault();
            }
            $(this).addClass('b-media-over');
            e.originalEvent.dataTransfer.dropEffect = 'move';
            return false;
        });

        $(".b-media__item").bind("dragleave", function () {
            $(this).removeClass('b-media-over');
        });

        $(".b-media__item").bind("drop", function (e) {
            if (e.originalEvent.stopPropagation) e.originalEvent.stopPropagation();

            $.post("/admin/CmsMedia/" + e.originalEvent.dataTransfer.getData("Action"), { newId: $(this).data("id"), id: e.originalEvent.dataTransfer.getData("Text") }, function (data) {
                if (data == 1) {
                    document.location.reload(true);
                }
            });

            return false;
        });
    });      
</script>